﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Randata.DataResolvers.Implementation
{
    internal class CustomObjectDataResolver<T> : DataResolver<T>
    {
        internal override T Resolve()
        {
            Type customType = typeof(T);
            T customObject = (T)customType.GetConstructor(Type.EmptyTypes).Invoke(new Object[] { });

            DataProvider.Instances.Add(customType, customObject);
            
            foreach (PropertyInfo prop in customType.GetProperties())
            {
                if (prop.CanWrite)
                {
                    Object value = DataProvider.Any(prop.PropertyType);

                    try
                    {
                        prop.SetValue(customObject, value, null);
                    }
                    catch
                    {
                        continue;
                    }
                }
            }

            return customObject;
        }
    }
}
